#pragma once

#include <QtGui/QMainWindow>
#include "ui_sim.h"
#include <QGraphicsView>
#include <QVBoxLayout>
#include <QStatusBar>
#include <QToolBar>
#include <QMenuBar>
#include <QTextEdit>
#include <QPushButton>
#include <QTabWidget>
#include "./qcustomplot.h"
//#include <QSlider>
//#include <QCombo>
//#include <QItemSelectionModel>
//#include <QTreeView>
class SceneView;
class PropertiesModel;
class Delegate;
class OilModel;
class TimeSettings;
class itemTab;
class MdiChild;
class QAction;
class QMenu;
class QMdiArea;
class QMdiSubWindow;
class QSignalMapper;
class OilModelEitor;

using namespace std;

class sim : public QMainWindow
{
	Q_OBJECT

public:
	sim(QWidget *parent = 0, Qt::WFlags flags = 0);
	~sim();
	void log(const QString &text);
	QTreeView *properties;
	void setPropertiesModel(PropertiesModel *model);
private:
	Ui::simClass ui;
	void init_interface();  
	void init_menu();
	void init_statusbar();
	void init_toolbox();
	void init_log();
	void init_properties();
	void save();
	void open();
	void clear_all();
	bool verify();
	MdiChild *activeMdiChild();
	void save_group(QSettings *settings, const QString &group_name, const vector<QStringList> &data, const vector<QStringList> &auxiliary_data);
	pair<vector<QStringList>, vector<QStringList>> open_group(QSettings *settings, const QString &group_name);
private:
	QString filePath;
    
	QStatusBar *statusbar;
	SceneView *scene;
	QGraphicsView *view;
	//QMenu *itemMenu;
	QVBoxLayout *layout;
	//QMenuBar *menuBar;
	//QToolBar *toolBarFile;
	QLabel *property_description;
	QTextEdit *logText;

	//menu
	QAction *NewDoc;
	QAction *OpenDoc;
	QAction *SaveDoc;
	QAction *SaveAsDoc;
	QAction *Quit;
	QAction *PrintDoc;
	QAction *CutItem;
	QAction *CopyItem;
	QAction *PasteItem;
	QAction *DeleteItem;
	QAction *Run;
	QAction *Verify;
    QAction *Plot;
	QAction *Test;
	QAction *tbFile;

	QAction *tbEdit;
	//QAction *tbMode;
	QAction *tbView;
	QAction *Properties;
	QAction *Scale50;
	QAction *Scale75;
	QAction *Scale100;
	QAction *Scale150;
	QAction *Scale250;
	QAction *ScaleUp;
	QAction *ScaleDown;
	QAction *Info;

	QMenu *File;
	QMenu *Edit;
	QMenu *Window;
	QMenu *View;
	QMenu *Information;
	QMenu *Execution;
	QMenu *Position;
	QMenu *Scale;

	QMenuBar *menuBar;
	QMenuBar *menuBarFile;
	QToolBar *toolBarFile;
	QMenuBar *menuBarEdit;
	QToolBar *toolBarEdit;
	QMenuBar *menuBarMode;
	QToolBar *toolBarExecution;
	QMenuBar *menuBarWindow;
	QMenuBar *menuBarView;
	QToolBar *toolBarView;
	QMenuBar *menuBarInformation;
	QToolBar *toolBarInformation;

	QLabel * scaleLabel;
	QSlider *scaleSlider;

	QMdiArea *mdiArea;
	QSignalMapper *windowMapper;

	vector<OilModel*> oilModelList;
	int oilModelIndex;
	TimeSettings *timeModel;
	//QSettings settings;
    vector<auto_ptr<pde_stationary_data>> pde_buffers;
private slots:
	MdiChild *createMdiChild();

	void scaleView(qreal scaleFactor);
	void scaleUp(){scaleView(1.1);}
	void scaleDown(){scaleView(0.9);}
	void scale50(){setZoom(50);}
	void scale75(){setZoom(75);}
	void scale100(){setZoom(100);}
	void scale150(){setZoom(150);}
	void scale250(){setZoom(250);}
	void setZoom(int percentZoom);
	void update_menu();
	void propertiesIndexChanged(const QModelIndex &current,const QModelIndex &previous);
	void displayOilModel(int index);
	void displayTimeSettingsModel();

	void bSave_clicked();
	void bSaveAs_clicked();
	void bOpen_clicked();
	void bNew_clicked();
	void bRun_clicked();
	void bVerify_clicked();
    void bPlot_clicked();
	void test();
	void setActiveSubWindow(QWidget *window);
	void oilModelEditorShow();
signals:
	void setDescription(QLabel *descr_label, QModelIndex sel_index);

};

class MdiChild : public QWidget {     
	Q_OBJECT
private:
	int graph_count;
	QWidget *wid;
	QVBoxLayout *vLayout;
	QCustomPlot * plot;
	sim *main;
	QString name;
public:
	MdiChild(sim *_main, const QString &_name);
	QString getName();
	public slots:
		void addZone(QVector<double> border_zones, QVector<QString> title_zones);
		void addPlot(const QVector<double> &x, const QVector<double> &y, const QString &name);
};

